<?php
/**
 * 导航控制器
 */
namespace Admin\Controller;
use Think\Controller;
class NaviController extends InitController {

	// 导航列表
	public function naviList() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		$naviModel = D('Navi');
		$total = $naviModel->getNaviNum(array('parent_id'=>0));
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		$list = $naviModel->getNaviList(array('parent_id'=>0),'taxis desc,id asc',$Page->firstRow.','.$Page->listRows,1);
		
		// 查询子类
		if(!empty($list)){
			foreach($list as $k=>$v){
				$list[$k]['sub'] = $naviModel->where(array('parent_id'=>$v['id']))->order("taxis desc,id asc")->select();
			}
		}
		
		$this->assign('list',$list);
		$this->assign('pager',$show);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Navi/naviList');
	}
	
	// 添加导航页
	public function naviAdd() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		$pid = I('pid',0,'intval');
	
		if ($pid) {
			$ptype = M('Navi')->where(array('id'=>$pid))->getField('type');
		}
		
		$this->assign('pid',$pid);
		$this->assign('ptype',$ptype);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Navi/naviAdd');
	}
	
	// 添加导航操作
	public function do_naviAdd() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$title = I('title','','strip_tags,htmlspecialchars,trim');
		if (empty($title)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入导航标题！','field'=>''));
		}
		$url = I('url','','strip_tags,trim');
		if (!empty($url) && !isUrl($url)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入有效的网址！','field'=>''));
		}
		$pid = I('pid',0,'intval');
		$type = I('type',0,'intval');
		$status = I('status',0,'intval');
		$taxis = I('taxis',100,'intval');
		
		$data['title'] = $title;
		$data['url'] = $url;
		$naviModel = M('Navi');
		if(!$pid){
			$data['type'] = $type;
		}else{
			$data['type'] = $naviModel->where(array('id'=>$pid))->getField('type');
		}
		$data['status'] = $status;
		$data['taxis'] = $taxis;
		$data['createtime'] = NOW_TIME;
		$data['admin_id'] = session('uid');
		$data['parent_id'] = $pid;
		
		if ($naviModel->add($data)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 编辑导航
	public function naviEdit() {
		$id = I('id',0,'intval');
		if (!id) {
			$this->error('参数错误');
		}
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$navi = D('Navi')->getNaviById($id);
		
		$this->assign('navi',$navi);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Navi/naviEdit');
	}
	
	// 编辑导航操作
	public function do_naviEdit() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		$title = I('title','','strip_tags,htmlspecialchars,trim');
		if (empty($title)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入导航标题！','field'=>''));
		}
		$url = I('url','','strip_tags,trim');
		if (!empty($url) && !isUrl($url)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入有效的网址！','field'=>''));
		}
		$type = I('type',0,'intval');
		$status = I('status',0,'intval');
		$taxis = I('taxis',100,'intval');
		
		$data['title'] = $title;
		$data['url'] = $url;
		$data['type'] = $type;
		$data['status'] = $status;
		$data['taxis'] = $taxis;
		
		if (M('Navi')->where(array('id'=>$id))->save($data) !== false) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 删除导航操作
	public function naviDel() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		if (M('Navi')->where(array('id'=>$id))->delete()) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	
}